package com.yyfax.pay.order.data.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.yyfax.commons.lang.DateUtil;
import com.yyfax.framework.service.CacheService;
import com.yyfax.framework.service.impl.BaseServiceImpl;
import com.yyfax.pay.common.constants.PayConstants;
import com.yyfax.pay.common.enums.PayTypeEnum;
import com.yyfax.pay.common.util.CommonIdUtils;
import com.yyfax.pay.order.data.dao.PayOrderDao;
import com.yyfax.pay.order.data.service.PayOrderExtService;
import com.yyfax.pay.order.data.service.PayOrderLogService;
import com.yyfax.pay.order.data.service.PayOrderService;
import com.yyfax.pay.order.model.po.PayOrderExtPO;
import com.yyfax.pay.order.model.po.PayOrderLogPO;
import com.yyfax.pay.order.model.po.PayOrderPO;
import com.yyfax.pay.order.model.vo.PayOrderVO;

/**
 * 支付订单服务实现
 *
 * @author buyi
 * @date 2017-04-12 11:09:15
 * @since 1.0.0
 */
@Service
public class PayOrderServiceImpl extends BaseServiceImpl implements PayOrderService {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private PayOrderDao payOrderDao;

	@Autowired
	private PayOrderExtService payOrderExtService;

	@Autowired
	private PayOrderLogService payOrderLogService;

	@Autowired
	private PayConstants payConstants;

	public PayOrderServiceImpl(@Qualifier("payCacheService") CacheService cacheService) {
		super(cacheService);
	}

	@Override
	public PayOrderPO queryById(int id, String merchantCode) throws Exception {
		if (id <= 0 && StringUtils.isBlank(merchantCode)) {
			logger.debug("参数 'id' 不能为0，且 'merchantCode' 不能为空 ");
			return null;
		}

		return payOrderDao.selectById(id, merchantCode);
	}

	@Override
	public String saveAndReturnBusiKey(PayOrderPO po) throws Exception {
		if (po == null) {
			logger.debug("参数po为空");
			return null;
		}

		String payOrderId = CommonIdUtils.generateId(PayTypeEnum.PAY, po.getMerchantCode(), po.getOrderId());// 根据策略生成业务主键

		po.setPayOrderId(payOrderId);

		if (StringUtils.isBlank(po.getExpireTime())) { // 如果没有传订单过期时间，则默认生成过期时间
			long expireTime = DateUtil.str2DateTime(po.getOrderTime()).getTime() + payConstants.getDefaultExpireTime();
			po.setExpireTime(DateUtil.milli2str(expireTime));
		}

		po.setCreateTime(DateUtil.getCurrentDateTime());

		int id = payOrderDao.insertReturnPK(po);
		po.setId(id);

		return payOrderId;
	}

	@Override
	public PayOrderPO queryByPayOrderId(String merchantCode, String payOrderId) {
		if (StringUtils.isBlank(payOrderId) && StringUtils.isBlank(merchantCode)) {
			logger.debug("参数 payOrderId/merchantCode 为空 ");
			return null;
		}

		return payOrderDao.selectByPayOrderId(payOrderId, merchantCode);
	}

	@Override
	public PayOrderPO queryByMerchantCodeAndOrderId(String merchantCode, String orderId) {
		if (StringUtils.isBlank(merchantCode) && StringUtils.isBlank(orderId)) {
			logger.debug("参数 orderId/merchantCode 为空 ");
			return null;
		}

		return payOrderDao.selectByOrderIdAndMerchantCode(orderId, merchantCode);
	}

	@Override
	public PayOrderVO queryPayOrderVO(String payOrderId, String merchantCode) throws Exception {
		PayOrderPO payOrderPO = queryByPayOrderId(merchantCode, payOrderId);
		if (payOrderPO == null) {
			return null;
		}

		PayOrderVO payOrderVO = new PayOrderVO();
		BeanUtils.copyProperties(payOrderPO, payOrderVO);

		PayOrderExtPO payOrderExtPO = payOrderExtService.queryByPayOrderId(payOrderId, merchantCode);
		payOrderVO.setPayOrderExtPO(payOrderExtPO);

		List<PayOrderLogPO> payOrderLogs = payOrderLogService.queryListByPayOrderId(payOrderId, merchantCode);
		payOrderVO.setOrderLogPOs(payOrderLogs);

		return payOrderVO;
	}

	@Override
	public PayOrderVO queryPayOrderVOByOrderId(String orderId, String merchantCode) throws Exception {
		PayOrderPO payOrderPO = queryByMerchantCodeAndOrderId(merchantCode, orderId);
		if (payOrderPO == null) {
			return null;
		}

		return queryPayOrderVO(payOrderPO.getPayOrderId(), merchantCode);
	}
}
